package com.fourth.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fourth.dto.Result;
import com.fourth.dto.UserDTO;
import com.fourth.entity.User;
import com.fourth.service.ICardBindingService;
import com.fourth.service.IUserService;
import com.fourth.util.UserHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 邓建波
 * @since 2022-08-04
 */
@Api("用户行为相关")
@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private IUserService iUserService;
    @Resource
    private ICardBindingService iCardBindingService;

    /**
     * 获取已绑定银行卡列表
    * @author 邓建波
    * @date 2022/8/5 17:14
    */
    @ApiOperation("获取用户绑卡列表")
    @PostMapping(value = "/getAllUserBindingCard")
    @ResponseBody
    public Result getAllUserBindingCard(){
        // TODO 获取 userId
        final Long userId = UserHolder.getUser().getUserId();
        // TODO 查询返回 前端处理如果data为null，提示绑卡跳转到绑卡页面
        return Result.ok("查询成功",iCardBindingService.query()
                .eq("user_id", userId).list());
    }
    /**
     * 从当前账户提现{amount}到银行卡{cardId}中
     *
     * @author 邓建波
     * @date 2022/8/4 15:53
     */
    @ApiOperation("用户提现")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "amount", value = "提现金额", paramType = "BigDecimal", readOnly = true),
            @ApiImplicitParam(name = "cardId", value = "银行卡号", paramType = "String", readOnly = true),
            @ApiImplicitParam(name = "payPassword", value = "支付密码", paramType = "String", readOnly = true)
    })
    @PostMapping(value = "/withdrawal/{amount}/{cardId}/{payPassword}")
    @ResponseBody
    public Result withdrawal(@PathVariable(value = "amount") BigDecimal amount,
                             @PathVariable(value = "cardId") String cardId,
                             @PathVariable(value = "payPassword") String payPassword
    ){
        return iUserService.withdrawal(amount,cardId,payPassword);
    }
    /**
     * 用当前银行卡{cardId}充值{amount}到账户中
     * @author 刘晨光
     * @date 2022/8/5 22:10
     */
    @ApiOperation("用户充值")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "amount", value = "充值金额", paramType = "BigDecimal", readOnly = true),
            @ApiImplicitParam(name = "cardId", value = "银行卡号", paramType = "String", readOnly = true),
            @ApiImplicitParam(name = "payPassword", value = "支付密码", paramType = "String", readOnly = true)
    })
    @PostMapping(value = "/recharge/{amount}/{cardId}/{payPassword}")
    @ResponseBody
    public Result recharge(@PathVariable(value = "amount") BigDecimal amount,
                           @PathVariable(value = "cardId") String cardId,
                           @PathVariable(value = "payPassword") String payPassword
    ){
        return iUserService.recharge(amount,cardId,payPassword);
    }

    /**
     * 获取用户名
    * @author 邓建波
    * @date 2022/8/5 17:15
    */
    @PostMapping("/getUserName")
    @ResponseBody
    public Result getUserName(){
        return Result.ok("查询成功",UserHolder.getUser().getUserName());
    }

    /**
     * 余额查询
     *
     * @author 余钊
     */
    @ApiOperation("余额查询")
    @RequestMapping(value = "/getBalance" ,method = RequestMethod.POST)
    @ResponseBody
    public Result balance() {
        //获取userHolder中的电子账户对象
        final UserDTO userDTO = UserHolder.getUser();

        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //SELECT balance FROM tb_user WHERE (user_id_card = ?)
        User user = iUserService.getOne(userLambdaQueryWrapper.select(User::getBalance)
                .eq(User::getUserId, userDTO.getUserId())
        );

        BigDecimal balance = user.getBalance();
        return Result.ok("查询成功",balance);
    }

    /**
     * 从付款账户转账{tradeMoney}到收款账户{collAccount}中
     *
     * @author 王丽丽
     * @date 2022/8/4 15:53
     */
    @ApiOperation("用户提现")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "transferMoney", value = "转账金额", paramType = "BigDecimal", readOnly = true),
            @ApiImplicitParam(name = "transferPhone", value = "对方手机号", paramType = "String", readOnly = true),
            @ApiImplicitParam(name = "payPassword", value = "支付密码", paramType = "String", readOnly = true)
    })
    @PostMapping(value = "/transferAccounts/{transferPhone}/{transferMoney}/{payPassword}/{transferName}")
    @ResponseBody
    public Result transferAccounts(@PathVariable(value = "transferPhone") String transferPhone,
                                   @PathVariable(value = "transferMoney") BigDecimal transferMoney,
                                   @PathVariable(value = "payPassword") String payPassword,
                                   @PathVariable(value = "transferName") String transferName
    ){
        System.out.println(transferName);
        return iUserService.transferAccounts(transferMoney,transferPhone,payPassword,transferName);
    }
}
